layui.define(['jquery', 'util'], function(exports) {
	var $ = layui.jquery;
	var util = layui.util;

	var obj = {
		ctxPath : "",
		addCtx : function(ctx) {
			if (this.ctxPath == "") {
				this.ctxPath = ctx;
			}
		},
		toUrl : function(url, target) {
			switch (target) {
			case '_blank':
				window.open(url);
				break;
			case '_parent':
				parent.location.href = url;
				break;
			case '_top':
				top.location.href = url;
				break;
			default:
				location.href = url;
				break;
			}
		},
		getUrlParam : function(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			return r != null ? decodeURI(r[2]) : null;
		},
		getImageUrl: function(uuid, pixel) {
			var url = obj.ctxPath + '/image/' + uuid;
			url = pixel ? url + '?pixel=' + pixel : url;
			return url;
		},
		hideItem: function(dom) {
			var $dom = $(dom);
			var verify = $dom.attr('lay-verify');
			if (verify) {
				$dom.data('lay-verify', verify);
				$dom.removeAttr('lay-verify');
			}
			$dom.parents('.layui-form-item').addClass('layui-hide');
		},
		showItem: function(dom) {
			var $dom = $(dom);
			var verify = $dom.data('lay-verify');
			if (verify) {
				$dom.attr('lay-verify', verify);
			}
			$dom.parents('.layui-form-item').removeClass('layui-hide');
		},
		getJSON: function(url, data, success) {
			$.ajax({
				url: url,
				type: 'GET',
				traditional: true,
				data: data,
				dataType: 'json',
				success: success
			});
		},
		uploadImageOption: function(field) {
			return {
				elem: '#' + field + 'Edit',
				url: this.ctxPath + '/image/upload',
				accept: 'images',
				acceptMime: 'image/*',
				done: function(data) {
					if (data.state == 'ok') {
						// 成功
						$('#' + field + 'Input').val(data.value);
						$('#' + field + 'Img').prop('src', data.url);
					} else {
						// 失败，弹出提示
						layui.use('layer', function(layer) {
							layer.msg(data.msg, {
								icon: 5,
								anim: 6
							});
						});
					}
				}
			};
		},
		tableImageTemplet: function(image, pixel, name) {
			return function(d) {
				var rImage = image.indexOf('.') == -1 ? d[image] : d[image.split('.')[0]][image.split('.')[1]];
				var rName = name.indexOf('.') == -1 ? d[name] : d[name.split('.')[0]][name.split('.')[1]];
				if (rImage) {
					return '<img class="table-image" src="' + obj.getImageUrl(rImage, pixel) + '" layer-src="' + obj.getImageUrl(rImage) + '" alt="' + rName + '">';
				} else {
					return '';
				}
			}
		},
		tableImagePhotos: function(isParent) {
			$('.table-image').each(function() {
				var $parent;
				if (isParent) {
					$parent = $(this);
				} else {
					$parent = $(this).parent();
				}
				layui.use('layer', function(layer) {
					layer.photos({
						photos: $parent,
						anim: 0,
						resize: false
					});
				});
		    });
		},
		tableDateTemplet: function(date, format) {
			return function(d) {
				return d[date] ? util.toDateString(d[date], format) : '';
			}
		},
		forbidBackSpace: function() {
			var fbs = function(e) {
				var ev = e || window.event; //获取event对象 
				var obj = ev.target || ev.srcElement; //获取事件源 
				var t = obj.type || obj.getAttribute('type'); //获取事件源类型 
				//获取作为判断条件的事件类型 
				var vReadOnly = obj.readOnly;
				var vDisabled = obj.disabled;
				//处理undefined值情况 
				vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;
				vDisabled = (vDisabled == undefined) ? true : vDisabled;
				//当敲Backspace键时，事件源类型为密码或单行、多行文本的， 
				//并且readOnly属性为true或disabled属性为true的，则退格键失效 
				var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);
				//当敲Backspace键时，事件源类型非密码或单行、多行文本的，则退格键失效 
				var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";
				//判断 
				if (flag2 || flag1) return false;
			};
			//禁止后退键 作用于Firefox、Opera
	        document.onkeypress = fbs;
	        //禁止后退键  作用于IE、Chrome
	        document.onkeydown = fbs;
		},
		renderDynamic: function(name) {
			layui.use(['laytpl', 'form'], function() {
				var laytpl = layui.laytpl;
				var form = layui.form;
				// 添加方法
				var tpl = $('#' + name + 'Tpl').html();
				var add = function(data) {
					data = data ? data : {};
					laytpl(tpl).render(data, function(html) {
						$('#' + name + 'Container').append(html);
						form.render();
					});
				};
				// 按钮点击事件
				$('#' + name + 'Btn').on('click', function() {
					add();
				});
				$('#' + name + 'Container').on('click', '.delete-btn', function() {
					$(this).parent().remove();
				});
				// 初始化数据
				var data = $('#' + name + 'Container').data('data');
				if (data) {
					$.each(data, function() {
						add(this);
					});
				}
			});
		}
	};
	
	exports('common', obj);
});
